home *** CD-ROM | disk | FTP | other *** search
/ Clickx 63 / Clickx 63.iso / software / multimedia / mirov204 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / viewSource.js < prev    next >
Encoding:
Text File  |  2008-05-09  |  17.1 KB  |  597 lines

  1. //@line 39 "/e/xr19rel/WINNT_5.2_Depend/mozilla/toolkit/components/viewsource/content/viewSource.js"
  2.  
  3. const pageLoaderIface = Components.interfaces.nsIWebPageDescriptor;
  4. const nsISelectionPrivate = Components.interfaces.nsISelectionPrivate;
  5. const nsISelectionController = Components.interfaces.nsISelectionController;
  6. var gBrowser = null;
  7. var gViewSourceBundle = null;
  8. var gPrefs = null;
  9.  
  10. var gLastLineFound = '';
  11. var gGoToLine = 0;
  12.  
  13. try {
  14.   var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  15.                               .getService(Components.interfaces.nsIPrefService);
  16.   gPrefs = prefService.getBranch(null);
  17. } catch (ex) {
  18. }
  19.  
  20. var gSelectionListener = {
  21.   timeout: 0,
  22.   notifySelectionChanged: function(doc, sel, reason)
  23.   {
  24.     // Coalesce notifications within 100ms intervals.
  25.     if (!this.timeout)
  26.       this.timeout = setTimeout(updateStatusBar, 100);
  27.   }
  28. }
  29.  
  30. function onLoadViewSource() 
  31. {
  32.   viewSource(window.arguments[0]);
  33.   document.commandDispatcher.focusedWindow = content;
  34. }
  35.  
  36. function getBrowser()
  37. {
  38.   if (!gBrowser)
  39.     gBrowser = document.getElementById("content");
  40.   return gBrowser;
  41. }
  42.  
  43. function getSelectionController()
  44. {
  45.   return getBrowser().docShell
  46.     .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  47.     .getInterface(Components.interfaces.nsISelectionDisplay)
  48.     .QueryInterface(nsISelectionController);
  49.  
  50. }
  51.  
  52. function getViewSourceBundle()
  53. {
  54.   if (!gViewSourceBundle)
  55.     gViewSourceBundle = document.getElementById("viewSourceBundle");
  56.   return gViewSourceBundle;
  57. }
  58.  
  59. function viewSource(url)
  60. {
  61.   if (!url)
  62.     return false; // throw Components.results.NS_ERROR_FAILURE;
  63.  
  64.   getBrowser().addEventListener("unload", onUnloadContent, true);
  65.   getBrowser().addEventListener("load", onLoadContent, true);
  66.  
  67.   var loadFromURL = true;
  68.   //
  69.   // Parse the 'arguments' supplied with the dialog.
  70.   //    arg[0] - URL string.
  71.   //    arg[1] - Charset value in the form 'charset=xxx'.
  72.   //    arg[2] - Page descriptor used to load content from the cache.
  73.   //    arg[3] - Line number to go to.
  74.   //    arg[4] - Whether charset was forced by the user
  75.   //
  76.   if ("arguments" in window) {
  77.     var arg;
  78.     //
  79.     // Set the charset of the viewsource window...
  80.     //
  81.     var charset;
  82.     if (window.arguments.length >= 2) {
  83.       arg = window.arguments[1];
  84.  
  85.       try {
  86.         if (typeof(arg) == "string" && arg.indexOf('charset=') != -1) {
  87.           var arrayArgComponents = arg.split('=');
  88.           if (arrayArgComponents) {
  89.             //we should "inherit" the charset menu setting in a new window
  90.             charset = arrayArgComponents[1];
  91.             gBrowser.markupDocumentViewer.defaultCharacterSet = charset;
  92.           }
  93.         }
  94.       } catch (ex) {
  95.         // Ignore the failure and keep processing arguments...
  96.       }
  97.     }
  98.     // If the document had a forced charset, set it here also
  99.     if (window.arguments.length >= 5) {
  100.       arg = window.arguments[4];
  101.  
  102.       try {
  103.         if (arg === true) {
  104.           var docCharset = getBrowser().docShell.QueryInterface
  105.                              (Components.interfaces.nsIDocCharset);
  106.           docCharset.charset = charset;
  107.         }
  108.       } catch (ex) {
  109.         // Ignore the failure and keep processing arguments...
  110.       }
  111.     }
  112.     //
  113.     // Get any specified line to jump to.
  114.     //
  115.     if (window.arguments.length >= 4) {
  116.       arg = window.arguments[3];
  117.       gGoToLine = parseInt(arg);
  118.     }
  119.     //
  120.     // Use the page descriptor to load the content from the cache (if
  121.     // available).
  122.     //
  123.     if (window.arguments.length >= 3) {
  124.       arg = window.arguments[2];
  125.  
  126.       try {
  127.         if (typeof(arg) == "object" && arg != null) {
  128.           var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  129.  
  130.           //
  131.           // Load the page using the page descriptor rather than the URL.
  132.           // This allows the content to be fetched from the cache (if
  133.           // possible) rather than the network...
  134.           //
  135.           PageLoader.loadPage(arg, pageLoaderIface.DISPLAY_AS_SOURCE);
  136.           // The content was successfully loaded from the page cookie.
  137.           loadFromURL = false;
  138.         }
  139.       } catch(ex) {
  140.         // Ignore the failure.  The content will be loaded via the URL
  141.         // that was supplied in arg[0].
  142.       }
  143.     }
  144.   }
  145.  
  146.   if (loadFromURL) {
  147.     // We need to set up session history to give us a page descriptor.
  148.     //
  149.     var webNavigation = getBrowser().webNavigation;
  150.     webNavigation.sessionHistory = Components.classes["@mozilla.org/browser/shistory;1"].createInstance();
  151.     //
  152.     // Currently, an exception is thrown if the URL load fails...
  153.     //
  154.     var loadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
  155.     var viewSrcUrl = "view-source:" + url;
  156.     webNavigation.loadURI(viewSrcUrl, loadFlags, null, null, null);
  157.   }
  158.  
  159.   //check the view_source.wrap_long_lines pref and set the menuitem's checked attribute accordingly
  160.   if (gPrefs) {
  161.     try {
  162.       var wraplonglinesPrefValue = gPrefs.getBoolPref("view_source.wrap_long_lines");
  163.  
  164.       if (wraplonglinesPrefValue)
  165.         document.getElementById('menu_wrapLongLines').setAttribute("checked", "true");
  166.     } catch (ex) {
  167.     }
  168.     try {
  169.       document.getElementById("menu_highlightSyntax").setAttribute("checked", gPrefs.getBoolPref("view_source.syntax_highlight"));
  170.     } catch (ex) {
  171.     }
  172.   } else {
  173.     document.getElementById("menu_highlightSyntax").setAttribute("hidden", "true");
  174.   }
  175.  
  176.   window._content.focus();
  177.  
  178.   return true;
  179. }
  180.  
  181. function onLoadContent()
  182. {
  183.   //
  184.   // If the view source was opened with a "go to line" argument.
  185.   //
  186.   if (gGoToLine > 0) {
  187.     goToLine(gGoToLine);
  188.     gGoToLine = 0;
  189.   }
  190.   document.getElementById('cmd_goToLine').removeAttribute('disabled');
  191.  
  192.   // Register a listener so that we can show the caret position on the status bar.
  193.   window._content.getSelection()
  194.    .QueryInterface(nsISelectionPrivate)
  195.    .addSelectionListener(gSelectionListener);
  196. }
  197.  
  198. function onUnloadContent()
  199. {
  200.   //
  201.   // Disable "go to line" while reloading due to e.g. change of charset
  202.   // or toggling of syntax highlighting.
  203.   //
  204.   document.getElementById('cmd_goToLine').setAttribute('disabled', 'true');
  205. }
  206.  
  207. function ViewSourceClose()
  208. {
  209.   window.close();
  210. }
  211.  
  212. function ViewSourceReload()
  213. {
  214.   const webNavigation = getBrowser().webNavigation;
  215.   webNavigation.reload(webNavigation.LOAD_FLAGS_BYPASS_PROXY | webNavigation.LOAD_FLAGS_BYPASS_CACHE);
  216. }
  217.  
  218. // Strips the |view-source:| for editPage()
  219. function ViewSourceEditPage()
  220. {
  221.   editPage(window.content.location.href.substring(12), window, false);
  222. }
  223.  
  224. // Strips the |view-source:| for saveURL()
  225. function ViewSourceSavePage()
  226. {
  227.   saveURL(window.content.location.href.substring(12), null, "SaveLinkTitle");
  228. }
  229.  
  230. function onEnterPP()
  231. {
  232.   var toolbox = document.getElementById("viewSource-toolbox");
  233.   toolbox.hidden = true;
  234. }
  235.  
  236. function onExitPP()
  237. {
  238.   var toolbox = document.getElementById("viewSource-toolbox");
  239.   toolbox.hidden = false;
  240. }
  241.  
  242. function getPPBrowser()
  243. {
  244.   return document.getElementById("content");
  245. }
  246.  
  247. function getNavToolbox()
  248. {
  249.   return document.getElementById("appcontent");
  250. }
  251.  
  252. function getWebNavigation()
  253. {
  254.   try {
  255.     return gBrowser.webNavigation;
  256.   } catch (e) {
  257.     return null;
  258.   }
  259. }
  260.  
  261. function ViewSourceGoToLine()
  262. {
  263.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  264.         .getService(Components.interfaces.nsIPromptService);
  265.   var viewSourceBundle = getViewSourceBundle();
  266.  
  267.   var input = {value:gLastLineFound};
  268.   for (;;) {
  269.     var ok = promptService.prompt(
  270.         window,
  271.         viewSourceBundle.getString("goToLineTitle"),
  272.         viewSourceBundle.getString("goToLineText"),
  273.         input,
  274.         null,
  275.         {value:0});
  276.  
  277.     if (!ok) return;
  278.  
  279.     var line = parseInt(input.value);
  280.  
  281.     if (!(line > 0)) {
  282.       promptService.alert(window,
  283.           viewSourceBundle.getString("invalidInputTitle"),
  284.           viewSourceBundle.getString("invalidInputText"));
  285.   
  286.       continue;
  287.     }
  288.  
  289.     var found = goToLine(line);
  290.  
  291.     if (found) {
  292.       break;
  293.     }
  294.  
  295.     promptService.alert(window,
  296.         viewSourceBundle.getString("outOfRangeTitle"),
  297.         viewSourceBundle.getString("outOfRangeText"));
  298.   }
  299. }
  300.  
  301. function goToLine(line)
  302. {
  303.   var viewsource = window._content.document.body;
  304.  
  305.   //
  306.   // The source document is made up of a number of pre elements with
  307.   // id attributes in the format <pre id="line123">, meaning that
  308.   // the first line in the pre element is number 123.
  309.   // Do binary search to find the pre element containing the line.
  310.   //
  311.   var pre;
  312.   for (var lbound = 0, ubound = viewsource.childNodes.length; ; ) {
  313.     var middle = (lbound + ubound) >> 1;
  314.     pre = viewsource.childNodes[middle];
  315.  
  316.     var firstLine = parseInt(pre.id.substring(4));
  317.  
  318.     if (lbound == ubound - 1) {
  319.       break;
  320.     }
  321.  
  322.     if (line >= firstLine) {
  323.       lbound = middle;
  324.     } else {
  325.       ubound = middle;
  326.     }
  327.   }
  328.  
  329.   var result = {};
  330.   var found = findLocation(pre, line, null, -1, false, result);
  331.  
  332.   if (!found) {
  333.     return false;
  334.   }
  335.  
  336.   var selection = window._content.getSelection();
  337.   selection.removeAllRanges();
  338.  
  339.   // In our case, the range's startOffset is after "\n" on the previous line.
  340.   // Tune the selection at the beginning of the next line and do some tweaking
  341.   // to position the focusNode and the caret at the beginning of the line.
  342.  
  343.   selection.QueryInterface(nsISelectionPrivate)
  344.     .interlinePosition = true;    
  345.  
  346.   selection.addRange(result.range);
  347.  
  348.   if (!selection.isCollapsed) {
  349.     selection.collapseToEnd();
  350.  
  351.     var offset = result.range.startOffset;
  352.     var node = result.range.startContainer;
  353.     if (offset < node.data.length) {
  354.       // The same text node spans across the "\n", just focus where we were.
  355.       selection.extend(node, offset);
  356.     }
  357.     else {
  358.       // There is another tag just after the "\n", hook there. We need
  359.       // to focus a safe point because there are edgy cases such as
  360.       // <span>...\n</span><span>...</span> vs.
  361.       // <span>...\n<span>...</span></span><span>...</span>
  362.       node = node.nextSibling ? node.nextSibling : node.parentNode.nextSibling;
  363.       selection.extend(node, 0);
  364.     }
  365.   }
  366.  
  367.   var selCon = getSelectionController();
  368.   selCon.setDisplaySelection(nsISelectionController.SELECTION_ON);
  369.   selCon.setCaretVisibilityDuringSelection(true);
  370.  
  371.   // Scroll the beginning of the line into view.
  372.   selCon.scrollSelectionIntoView(
  373.     nsISelectionController.SELECTION_NORMAL,
  374.     nsISelectionController.SELECTION_FOCUS_REGION,
  375.     true);
  376.  
  377.   gLastLineFound = line;
  378.  
  379.   document.getElementById("statusbar-line-col").label =
  380.     getViewSourceBundle().getFormattedString("statusBarLineCol", [line, 1]);
  381.  
  382.   return true;
  383. }
  384.  
  385. function updateStatusBar()
  386. {
  387.   // Reset the coalesce flag.
  388.   gSelectionListener.timeout = 0;
  389.  
  390.   var statusBarField = document.getElementById("statusbar-line-col");
  391.  
  392.   var selection = window._content.getSelection();
  393.   if (!selection.focusNode) {
  394.     statusBarField.label = '';
  395.     return;
  396.   }
  397.   if (selection.focusNode.nodeType != Node.TEXT_NODE) {
  398.     return;
  399.   }
  400.  
  401.   var selCon = getSelectionController();
  402.   selCon.setDisplaySelection(nsISelectionController.SELECTION_ON);
  403.   selCon.setCaretVisibilityDuringSelection(true);
  404.  
  405.   var interlinePosition = selection
  406.       .QueryInterface(nsISelectionPrivate).interlinePosition;
  407.  
  408.   var result = {};
  409.   findLocation(null, -1, 
  410.       selection.focusNode, selection.focusOffset, interlinePosition, result);
  411.  
  412.   statusBarField.label = getViewSourceBundle()
  413.       .getFormattedString("statusBarLineCol", [result.line, result.col]);
  414. }
  415.  
  416. //
  417. // Loops through the text lines in the pre element. The arguments are either
  418. // (pre, line) or (node, offset, interlinePosition). result is an out
  419. // argument. If (pre, line) are specified (and node == null), result.range is
  420. // a range spanning the specified line. If the (node, offset,
  421. // interlinePosition) are specified, result.line and result.col are the line
  422. // and column number of the specified offset in the specified node relative to
  423. // the whole file.
  424. //
  425. function findLocation(pre, line, node, offset, interlinePosition, result)
  426. {
  427.   if (node && !pre) {
  428.     //
  429.     // Look upwards to find the current pre element.
  430.     //
  431.     for (pre = node;
  432.          pre.nodeName != "PRE";
  433.          pre = pre.parentNode);
  434.   }
  435.  
  436.   //
  437.   // The source document is made up of a number of pre elements with
  438.   // id attributes in the format <pre id="line123">, meaning that
  439.   // the first line in the pre element is number 123.
  440.   //
  441.   var curLine = parseInt(pre.id.substring(4));
  442.  
  443.   //
  444.   // Walk through each of the text nodes and count newlines.
  445.   //
  446.   var treewalker = window._content.document
  447.       .createTreeWalker(pre, NodeFilter.SHOW_TEXT, null, false);
  448.  
  449.   //
  450.   // The column number of the first character in the current text node.
  451.   //
  452.   var firstCol = 1;
  453.  
  454.   var found = false;
  455.   for (var textNode = treewalker.firstChild();
  456.        textNode && !found;
  457.        textNode = treewalker.nextNode()) {
  458.  
  459.     //
  460.     // \r is not a valid character in the DOM, so we only check for \n.
  461.     //
  462.     var lineArray = textNode.data.split(/\n/);
  463.     var lastLineInNode = curLine + lineArray.length - 1;
  464.  
  465.     //
  466.     // Check if we can skip the text node without further inspection.
  467.     //
  468.     if (node ? (textNode != node) : (lastLineInNode < line)) {
  469.       if (lineArray.length > 1) {
  470.         firstCol = 1;
  471.       }
  472.       firstCol += lineArray[lineArray.length - 1].length;
  473.       curLine = lastLineInNode;
  474.       continue;
  475.     }
  476.  
  477.     //
  478.     // curPos is the offset within the current text node of the first
  479.     // character in the current line.
  480.     //
  481.     for (var i = 0, curPos = 0;
  482.          i < lineArray.length;
  483.          curPos += lineArray[i++].length + 1) {
  484.  
  485.       if (i > 0) {
  486.         curLine++;
  487.       }
  488.  
  489.       if (node) {
  490.         if (offset >= curPos && offset <= curPos + lineArray[i].length) {
  491.           //
  492.           // If we are right after the \n of a line and interlinePosition is
  493.           // false, the caret looks as if it were at the end of the previous
  494.           // line, so we display that line and column instead.
  495.           //
  496.           if (i > 0 && offset == curPos && !interlinePosition) {
  497.             result.line = curLine - 1;
  498.             var prevPos = curPos - lineArray[i - 1].length;
  499.             result.col = (i == 1 ? firstCol : 1) + offset - prevPos;
  500.  
  501.           } else {
  502.             result.line = curLine;
  503.             result.col = (i == 0 ? firstCol : 1) + offset - curPos;
  504.           }
  505.           found = true;
  506.  
  507.           break;
  508.         }
  509.  
  510.       } else {
  511.         if (curLine == line && !("range" in result)) {
  512.           result.range = document.createRange();
  513.           result.range.setStart(textNode, curPos);
  514.  
  515.           //
  516.           // This will always be overridden later, except when we look for
  517.           // the very last line in the file (this is the only line that does
  518.           // not end with \n).
  519.           //
  520.           result.range.setEndAfter(pre.lastChild);
  521.  
  522.         } else if (curLine == line + 1) {
  523.           result.range.setEnd(textNode, curPos - 1);
  524.           found = true;
  525.           break;
  526.         }
  527.       }
  528.     }
  529.   }
  530.  
  531.   return found || ("range" in result);
  532. }
  533.  
  534. //function to toggle long-line wrapping and set the view_source.wrap_long_lines 
  535. //pref to persist the last state
  536. function wrapLongLines()
  537. {
  538.   var myWrap = window._content.document.body;
  539.  
  540.   if (myWrap.className == '')
  541.     myWrap.className = 'wrap';
  542.   else myWrap.className = '';
  543.  
  544.   //since multiple viewsource windows are possible, another window could have 
  545.   //affected the pref, so instead of determining the new pref value via the current
  546.   //pref value, we use myWrap.className  
  547.   if (gPrefs){
  548.     try {
  549.       if (myWrap.className == '') {
  550.         gPrefs.setBoolPref("view_source.wrap_long_lines", false);
  551.       }
  552.       else {
  553.         gPrefs.setBoolPref("view_source.wrap_long_lines", true);
  554.       }
  555.     } catch (ex) {
  556.     }
  557.   }
  558. }
  559.  
  560. //function to toggle syntax highlighting and set the view_source.syntax_highlight
  561. //pref to persist the last state
  562. function highlightSyntax()
  563. {
  564.   var highlightSyntaxMenu = document.getElementById("menu_highlightSyntax");
  565.   var highlightSyntax = (highlightSyntaxMenu.getAttribute("checked") == "true");
  566.   gPrefs.setBoolPref("view_source.syntax_highlight", highlightSyntax);
  567.  
  568.   var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  569.   PageLoader.loadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  570. }
  571.  
  572. // Fix for bug 136322: this function overrides the function in
  573. // browser.js to call PageLoader.loadPage() instead of BrowserReloadWithFlags()
  574. function BrowserSetForcedCharacterSet(aCharset)
  575. {
  576.   var docCharset = getBrowser().docShell.QueryInterface(
  577.                             Components.interfaces.nsIDocCharset);
  578.   docCharset.charset = aCharset;
  579.   var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  580.   PageLoader.loadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  581. }
  582.  
  583. // fix for bug #229503
  584. // we need to define BrowserSetForcedDetector() so that we can
  585. // change auto-detect options in the "View | Character Encoding" menu.
  586. // As with BrowserSetForcedCharacterSet(), call PageLoader.loadPage() 
  587. // instead of BrowserReloadWithFlags()
  588. function BrowserSetForcedDetector(doReload)
  589. {
  590.   getBrowser().documentCharsetInfo.forcedDetector = true; 
  591.   if (doReload)
  592.   {
  593.     var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  594.     PageLoader.loadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  595.   }
  596. }
  597.